package com.novel.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import com.novel.bean.BookBean;
import com.novel.bean.SectionBean;

public enum NovelDao {
	novelDAO;
	
	public long insertBook(Connection conn, BookBean book)throws Exception{
		StringBuffer sql = new StringBuffer("insert into xs_book_check(category_id,name,intro,author,serialize,");
		sql.append("create_time,cover) ");
		sql.append(" values(?,?,?,?,?,?,?)");
		PreparedStatement ps = null;
		ResultSet rs = null;
		long id = 0;
	    try{
	    	ps = conn.prepareStatement(sql.toString(),Statement.RETURN_GENERATED_KEYS);
	    	
	    	ps.setLong(1, book.getCategoryid());
	    	ps.setString(2, book.getName());
	    	ps.setString(3, book.getIntro());
	    	ps.setString(4, book.getAuthor());
	    	ps.setInt(5, book.getSerialize());
	    	ps.setString(6, book.getCreatetime());
	    	ps.setString(7, book.getCover());
	    	
	    	ps.executeUpdate();
	    	rs=ps.getGeneratedKeys();
	    	rs.next();
	    	id=rs.getLong(1);
	    	ps.close();
	    }catch(Exception e){
	    	System.out.println("插入书籍发生异常："+e.getMessage());
	    	throw new Exception();
	    }
	    return id;
	}
	public void insertSection(Connection conn, SectionBean section)throws Exception{
		StringBuffer sql = new StringBuffer("insert into xs_book_section_check(book_id,content,create_time,name,num)");
		sql.append(" values(?,?,?,?,?)");
		PreparedStatement ps = null;
		try{
			ps = conn.prepareStatement(sql.toString());
			
			ps.setLong(1, section.getBookid());
			ps.setString(2, section.getContent());
			ps.setString(3, section.getCreatetime());
			ps.setString(4, section.getName());		
			ps.setInt(5, section.getNum());		
			ps.executeUpdate();
			ps.close();
		}catch(Exception e){
			System.out.println("插入章节发生异常："+e.getMessage());
			throw new Exception();
		}
	}
}
